var express = require('express'),
    router = express.Router(),
    request = require('request');
router.post('/*',function (req, res,next) {
    res.httpOnly=false;
    request({
            method: req.method,
            preambleCRLF: true,
            postambleCRLF: true,
            uri: config.resourcePath.apiRoot + req.url,
            formData: req.body
        },
        function (error, response, body) {
            if (error) {
                res.send(error);
            } else {
                if (req.query.m == 'Login') {
                    try {
                        var result = JSON.parse(body);
                        console.log(result);
                        if(result.Code==200) {
                            // 1000 * 60 * 60 * 24 * 140
                            var cookieTime= 1000 * 60 * 60 * 1;
                            if(result.accoun && result.account!=undefined) {
                                res.cookie('account', result.account, {
                                    maxAge: cookieTime,
                                    httpOnly: false
                                });
                            }
                            if(result.default_auto && result.default_auto!=undefined) {
                                res.cookie('default_auto', result.default_auto, {
                                    maxAge: cookieTime,
                                    httpOnly: false
                                });
                            }
                            if(result.u_id && result.u_id!=undefined) {
                                res.cookie('u_id', result.u_id, {maxAge: cookieTime, httpOnly: false});
                            }
                            if(result.access_token && result.access_token!=undefined) {
                                res.cookie('access_token', result.access_token, {maxAge: cookieTime, httpOnly: false});
                            }
                            if(result.account && result.account!=undefined) {
                                res.cookie('account', result.account, {maxAge: cookieTime, httpOnly: false});
                            }
                            if(result.userInfo && result.userInfo!='undefined') {
                                for (var i in result.userInfo) {
                                    if(i=='memberInfo'){
                                        var memberInfo=result.userInfo['memberInfo'];
                                        for (var j in memberInfo) {
                                            res.cookie(j, memberInfo[j] || '', {
                                                maxAge: cookieTime,
                                                httpOnly: false
                                            });
                                        }
                                    }else {
                                        res.cookie(i, result.userInfo[i] || '', {
                                            maxAge: cookieTime,
                                            httpOnly: false
                                        });
                                    }
                                }
                            }
                        }
                    } catch (e) {

                    }
                }
                res.send(body);
            }
        });
});
module.exports = router;

